<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>分析结果 - AI视频监控系统</title>
    <style>
        /* 全局样式 */
        body {
            font-family: 'Arial', sans-serif;
            line-height: 1.6;
            color: #333;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        
        .container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            margin-bottom: 20px;
        }
        
        h1, h2, h3 {
            color: #2c3e50;
        }
        
        /* 导航栏 */
        .navbar {
            display: flex;
            justify-content: space-between;
            padding: 10px 0;
            margin-bottom: 20px;
            border-bottom: 1px solid #eee;
        }
        
        .navbar a {
            color: #3498db;
            text-decoration: none;
            padding: 5px 10px;
            border-radius: 4px;
        }
        
        .navbar a:hover {
            background-color: #f0f9ff;
        }
        
        /* 结果卡片样式 */
        .result-card {
            border: 1px solid #e0e0e0;
            border-radius: 5px;
            padding: 15px;
            margin-bottom: 15px;
            background-color: white;
        }
        
        .alert {
            background-color: #ffecec;
            border-left: 4px solid #f44336;
        }
        
        .normal {
            background-color: #e8f5e9;
            border-left: 4px solid #4caf50;
        }
        
        /* 预览区域 */
        .media-preview {
            margin-top: 15px;
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
        }
        
        .media-preview img, .media-preview video {
            max-width: 100%;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }
        
        /* 加载动画 */
        .loading {
            display: block;
            margin: 20px auto;
            text-align: center;
        }
        
        .loading-spinner {
            border: 5px solid #f3f3f3;
            border-top: 5px solid #3498db;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 2s linear infinite;
            margin: 0 auto;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        /* 按钮样式 */
        .btn {
            display: inline-block;
            padding: 10px 20px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
            text-decoration: none;
            margin-right: 10px;
        }
        
        .btn:hover {
            background-color: #2980b9;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="navbar">
            <h1>AI视频监控系统 - 分析结果</h1>
            <a href="/" class="btn">返回首页</a>
        </div>
        
        <div id="loading" class="loading">
            <div class="loading-spinner"></div>
            <p>正在加载分析结果...</p>
        </div>
        
        <div id="results" style="display: none;">
            <div id="summary"></div>
            <div id="segmentResults"></div>
        </div>
    </div>
    
    <script>
        // 获取元素
        const loading = document.getElementById('loading');
        const results = document.getElementById('results');
        const summary = document.getElementById('summary');
        const segmentResults = document.getElementById('segmentResults');
        
        // 页面加载时获取结果
        document.addEventListener('DOMContentLoaded', async () => {
            try {
                const response = await fetch('/api/results');
                const data = await response.json();
                
                if (data.error) {
                    alert(data.error);
                    loading.style.display = 'none';
                    return;
                }
                
                displayResults(data);
            } catch (error) {
                console.error('Error loading results:', error);
                alert('加载结果失败: ' + error.message);
                loading.style.display = 'none';
            }
        });
        
        // 显示分析结果
        function displayResults(data) {
            loading.style.display = 'none';
            results.style.display = 'block';
            
            // 显示结果摘要
            summary.innerHTML = `
                <h3>视频: ${data.filename}</h3>
                <p>${data.summary}</p>
                <p>共分析了 ${data.segments.length} 个视频片段</p>
            `;
            
            // 清空之前的结果
            segmentResults.innerHTML = '';
            
            // 显示每个片段的结果
            data.segments.forEach(segment => {
                const segmentCard = document.createElement('div');
                segmentCard.className = `result-card ${segment.has_alert ? 'alert' : 'normal'}`;
                
                let cardContent = `
                    <h3>片段 #${segment.segment_id}</h3>
                    <p>时间范围: ${segment.start_time} 至 ${segment.end_time}</p>
                `;
                
                if (segment.has_alert) {
                    cardContent += `
                        <div>${segment.alert}</div>
                        <div>${segment.description}</div>
                        <div class="media-preview">
                    `;
                    
                    if (segment.image_file) {
                        cardContent += `<img src="/video_warning/${segment.image_file}" alt="Warning Screenshot">`;
                    }
                    
                    if (segment.video_file) {
                        cardContent += `
                            <video controls>
                                <source src="/video_warning/${segment.video_file}" type="video/mp4">
                                Your browser does not support the video tag.
                            </video>
                        `;
                    }
                    
                    cardContent += `</div>`;
                } else {
                    cardContent += `<p>正常片段 - 未检测到异常</p>`;
                }
                
                segmentCard.innerHTML = cardContent;
                segmentResults.appendChild(segmentCard);
            });
        }
    </script>
</body>
</html>
